//
//  TruckTypeViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/13.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "TruckTypeViewController.h"
#import "TruckTypeModel.h"
#import "LengthModel.h"
static NSString *cellPath = @"TruckTypeCell";
@interface TruckTypeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *allTypeAry;
@property (nonatomic, assign)NSInteger TruckType;
@property (nonatomic, assign)NSInteger lenthType;

@end

@implementation TruckTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _TruckType = 0;
    _lenthType = 0;
    [self creatUI];
    [self requestTruckType];
}

- (void)creatUI
{
    self.navigationItem.title = @"信息筛选";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-65*displaywidth-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-55*displaywidth, SCREEN_WIDTH-30*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureBtnAction) Title:@"确认" Font:16.0];
    soureBtn.backgroundColor = RGB(255, 153, 0);
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.layer.cornerRadius = 5;
    soureBtn.layer.masksToBounds = YES;
    [self.view addSubview:soureBtn];
}

- (void)soureBtnAction
{
    NSString *truckID = @"";
    NSString *lenthStr = @"";
    if (_TruckType != 0)
    {
        TruckTypeModel *model = _allTypeAry[_TruckType-1];
        truckID = s_str(model.ID);
        if (_lenthType != 0)
        {
            NSArray *lenthAry = model.truckLengths;
            LengthModel *lenModel = lenthAry[_lenthType-1];
            lenthStr = s_str(lenModel.length);
        }
    }
    if (self.closeBlock != nil)
    {
        self.closeBlock(truckID, lenthStr);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.section == 0)
    {
        UILabel *nameLabel = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 15*displaywidth, SCREEN_WIDTH-30*displaywidth, 20*displaywidth) Font:15.0 Text:@"车型选择" Color:BlaTitColor];
        [cell.contentView addSubview:nameLabel];
        CGFloat W = (SCREEN_WIDTH-70*displaywidth)/4;
        NSInteger num = _allTypeAry.count+1;
        for (int i = 0; i < _allTypeAry.count+1; i++)
        {
            NSString *name = @"不限";
            if (i != 0)
            {
                TruckTypeModel *model = _allTypeAry[i-1];
                name = model.name;
            }
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth+(W+10*displaywidth)*(i%4), 50*displaywidth+45*displaywidth*(i/4), W, 30*displaywidth) ImageName:nil Target:self Action:@selector(closeTruckAction:) Title:name Font:16.0];
            btn.layer.cornerRadius = 5;
            btn.layer.masksToBounds = YES;
            UIColor *backgroungColors = RGB(232, 232, 232);
            UIColor *titColor = RGB(102, 102, 102);
            if (i == _TruckType)
            {
                backgroungColors = ThemeColor;
                titColor = [UIColor whiteColor];
            }
            [btn setTitleColor:titColor forState:UIControlStateNormal];
            btn.backgroundColor = backgroungColors;
            btn.tag = 500+i;
            [cell.contentView addSubview:btn];
        }
        NSInteger line = num/4;
        if (num %4 != 0)
        {
            line+=1;
        }
        CGFloat H = 55*displaywidth+line*45*displaywidth;
        UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, H-1*displaywidth, SCREEN_WIDTH-30*displaywidth, 1*displaywidth)];
        lineAview.backgroundColor = RGB(238, 238, 238);
        [cell.contentView addSubview:lineAview];
    }
    else
    {
        UILabel *nameLabel = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 15*displaywidth, SCREEN_WIDTH-30*displaywidth, 20*displaywidth) Font:15.0 Text:@"车长选择" Color:BlaTitColor];
        [cell.contentView addSubview:nameLabel];
        CGFloat W = (SCREEN_WIDTH-70*displaywidth)/4;
        NSInteger num = 1;
        if (_TruckType != 0)
        {
            TruckTypeModel *model = _allTypeAry[_TruckType-1];
            NSArray *lenthAry = model.truckLengths;
            num+=lenthAry.count;
        }
        for (int i = 0; i < num; i++)
        {
            NSString *name = @"不限";
            if (i != 0)
            {
                TruckTypeModel *model = _allTypeAry[_TruckType-1];
                NSArray *lenthAry = model.truckLengths;
                LengthModel *lenModel = lenthAry[i-1];
                name = lenModel.length;
            }
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth+(W+10*displaywidth)*(i%4), 50*displaywidth+45*displaywidth*(i/4), W, 30*displaywidth) ImageName:nil Target:self Action:@selector(closeLenthAction:) Title:name Font:16.0];
            btn.layer.cornerRadius = 5;
            btn.layer.masksToBounds = YES;
            UIColor *backgroungColors = RGB(232, 232, 232);
            UIColor *titColor = RGB(102, 102, 102);
            if (i == _lenthType)
            {
                backgroungColors = ThemeColor;
                titColor = [UIColor whiteColor];
            }
            [btn setTitleColor:titColor forState:UIControlStateNormal];
            btn.backgroundColor = backgroungColors;
            btn.tag = 1000+i;
            [cell.contentView addSubview:btn];
        }
        NSInteger line = num/4;
        if (num %4 != 0)
        {
            line+=1;
        }
        CGFloat H = 55*displaywidth+line*45*displaywidth;
        UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, H-1*displaywidth, SCREEN_WIDTH-30*displaywidth, 1*displaywidth)];
        lineAview.backgroundColor = RGB(238, 238, 238);
        [cell.contentView addSubview:lineAview];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSInteger num = _allTypeAry.count+1;
        NSInteger line = num/4;
        if (num %4 != 0)
        {
            line+=1;
        }
        CGFloat H = 55*displaywidth+line*45*displaywidth;
        return H;
    }
    else
    {
        NSInteger num = 1;
        if (_TruckType != 0)
        {
            TruckTypeModel *model = _allTypeAry[_TruckType-1];
            NSArray *lenthAry = model.truckLengths;
            num+=lenthAry.count;
        }
        NSInteger line = num/4;
        if (num %4 != 0)
        {
            line+=1;
        }
        CGFloat H = 55*displaywidth+line*45*displaywidth;
        return H;
    }
}

- (void)closeTruckAction:(UIButton *)btn
{
    NSInteger index = btn.tag-500;
    if (index != _TruckType)
    {
        _TruckType = index;
        _lenthType = 0;
        [_tableview reloadData];
    }
    
}

- (void)closeLenthAction:(UIButton *)btn
{
    NSInteger index = btn.tag-1000;
    if (index != _lenthType)
    {
        _lenthType = index;
        [_tableview reloadData];
    }
    
}

- (void)requestTruckType
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_truckType Parmers:nil Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            _allTypeAry = [TruckTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
